package animationEngine;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/**
 * The Class FullStringBoxDisplay, this class represents the full view display which displays
 * the entire string and pattern on screen (zooming out to an appropriate size).
 * 
 * @author Andrei Palade
 * @author David McBrierty
 * @author Garry Sharp
 * @author Hristo Georgiev
 * @author John Dennis
 * @author Marco Sarconi
 * @version 1.0.1
 */
public class FullStringBoxDisplay extends JInternalFrame {

	/** Generated serial version ID. */
	private static final long serialVersionUID = -7308549895315390916L;

	//The string box to hold the string that is being searched
	/** The text box. */
	public StringBox textBox;
	//The panel to hold the textBox StringBox
	/** The text box panel. */
	private JPanel textBoxPanel;
	//The StringBox to hold the pattern
	/** The pattern box. */
	public StringBox patternBox;
	//The panel to hold the patternBox StringBox
	/** The pattern box panel. */
	public JPanel patternBoxPanel;

	/** The swap button panel. */
	public JPanel swapButtonPanel;
	
	/** The swap button. */
	public JButton swapButton;

	/** The old size. */
	final int oldSize = this.getWidth();

	/**
	 * Instantiates a new full string box display.
	 *
	 * @param text the text to be searched
	 * @param pattern the pattern to be searched for
	 */
	public FullStringBoxDisplay(String text,String pattern){
		super("Full Box", true, false, false, true);		

		//Set Layout to null so we can position the panels manually
		this.setLayout(new GridBagLayout());	

		GridBagConstraints constraints = new GridBagConstraints();


		textBoxPanel = new JPanel();
		patternBoxPanel = new JPanel();

		this.textBox = new StringBox(text,text.length());
		this.patternBox = new StringBox(pattern, pattern.length());

		//Set the PANELS location
		textBoxPanel.setLocation(0, 0);
		textBoxPanel.setSize(textBox.getSize().width ,textBox.getSize().height);
		textBox.setLocation(0, 0);
		textBoxPanel.add(textBox);

		//Add rules for the GridBagLayout
		//constraints.anchor = GridBagConstraints.NORTH;
		constraints.anchor = GridBagConstraints.BASELINE_LEADING;
		constraints.gridy = 0;
		constraints.gridx = 0;


		this.add(textBoxPanel,constraints);
		
		patternBoxPanel.setLocation(0, 0);
		patternBoxPanel.setSize(patternBox.getSize().width ,patternBox.getSize().height);
		patternBox.setLocation(0,0);
		patternBoxPanel.add(patternBox);


		constraints.anchor = GridBagConstraints.BASELINE_LEADING;
		constraints.gridx = 0;
		constraints.gridy = 1;

		this.add(patternBoxPanel, constraints);

		//Set out preferred size so that it can hold the pattern and text box
		this.setPreferredSize(new Dimension(this.textBox.getSize().width, 3*this.textBox.getSize().height));

		//Set our location in our parent container
		this.setLocation(0, 400);
		this.pack();
		this.setVisible(true);	

	}


	/**
	 * Sets the pattern and text.
	 *
	 * @param pattern the pattern to be searched for
	 * @param text the text to be searched
	 */
	public void setPatternAndText(String pattern, String text) {
		this.updateUI();
		patternBoxPanel.removeAll();		
		patternBox = new StringBox(pattern,pattern.length());
		patternBox.setLocation(0, 0);
		patternBoxPanel.add(patternBox);
		patternBoxPanel.setLocation(0, 0);

		textBoxPanel.removeAll();
		textBox = new StringBox(text,text.length());
		textBox.setLocation(0, 0);
		textBoxPanel.add(textBox);

		System.out.format("New pattern: %s\nNew Text: %s\n", pattern, text);
	}


	/**
	 * This method is used to shift the pattern the given distance inside us.
	 * The actual container that moves is the panel that holds the patternBox in it.
	 * 
	 * @param distance Integer representing the distance to move (in pixels).
	 */
	public void shiftPattern(int distance){
		//If we are trying to move the pattern forward
		if (distance > 0){
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (patternBoxPanel.getLocation().x + distance + patternBoxPanel.getWidth() > this.getWidth()){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(this.patternBoxPanel.getLocation().x + distance, this.patternBoxPanel.getLocation().y);
		}
		else {
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (patternBoxPanel.getLocation().x + distance < 0){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(this.patternBoxPanel.getLocation().x + distance, this.patternBoxPanel.getLocation().y);
		}
	}

	/**
	 * Move pattern to a given location.
	 *
	 * @param location the location to move the pattern to (in pixels).
	 */
	public void movePatternTo(int location){

		//If we are trying to move the pattern forward
		if (location > 0){
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (location + patternBoxPanel.getWidth() > this.getWidth()){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(location, this.patternBoxPanel.getLocation().y);
		}
		else {
			//Check that there is enough room to shift it forward (ie it wont go off the panel)
			if (location < 0){
				//System.out.println("Shifting pattern will take it out of view");
				return;
			}
			//Move the pattern
			this.patternBoxPanel.setLocation(location, this.patternBoxPanel.getLocation().y);
		}
	}

	/**
	 * This method is used to reset the pattern to the start of the currently displayed string.
	 */
	public void resetPattern(){
		//this.patternBoxPanel.setLocation(0, this.patternBoxPanel.getLocation().y);
		//this.patternBox.setLocation(0, this.patternBox.getLocation().y);
	}

	/**
	 * This method is used to reset the current index of the String to 0, which will
	 * place the string back at the start.
	 */
	public void resetString(){
		this.textBox.resetCurrentIndex();
	}

	/**
	 * This method is used to shift the string the given distance.
	 *
	 * @param distance the distance to shift the string (in pixels)
	 */
	public void shiftString(int distance){
		this.textBox.incrementCurrentIndex();
		this.textBox.reconstruct();
		//this.textBoxPanel.setLocation(0, 15);
		//this.textBox.setLocation(0, 15);
	}


	/**
	 * Clear the highlighted characters in the text and pattern box.
	 */
	public void clearHighlighting(){
		this.textBox.clearAllHighlighting();
		this.patternBox.clearAllHighlighting();
	}




}
